﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Collections;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Reflection;

namespace SmartProject.Data
{
    /// <summary>
    /// This class acts as the Data Access Layer and wraps all the data calls into 
    /// a simple helper class.
    /// </summary>
    public static class DataProvider
    {
        private static readonly string _connectionString;
        static DataProvider()
        {
            _connectionString = ConfigurationManager.ConnectionStrings["SmartProjectConnectionString"].ConnectionString;
        }
        /// <summary>
        /// Gets a new instance of the selected DataProvider.
        /// </summary>
        public static IDataProvider Instance
        {
            get
            {
                string assemblyDll = ConfigurationManager.AppSettings["SmartProjectDatabaseProvider"];
                Assembly assembly;
                if (!String.IsNullOrEmpty(assemblyDll))
                {
                    assembly = Assembly.Load(assemblyDll);
                }
                else
                {
                    assembly = Assembly.GetExecutingAssembly();
                }

                return (IDataProvider)assembly.CreateInstance(
                    ConfigurationManager.AppSettings["SmartProjectDatabaseProviderTypeName"]);
            }
        }

        /// <summary>
        /// Gets the global connection string used by SmartProject to the selected database.
        /// </summary>
        public static string ConnectionString
        {
            get { return _connectionString; }
        }
    }
}
